package OfferOneHundred;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @version: 1.0
 * @Author: wuqiu
 * @date: 2023-06-30 12:08
 * @description: LeetcodeLearnAlgorithm -> OfferOneHundred
 */
public class Q3数组中重复的数字 {
    public static void main(String[] args) {
        Q3数组中重复的数字 test = new Q3数组中重复的数字();
        test.findRepeatNumber3(new int[] {2, 3, 1, 0, 2, 5, 3});
    }

    public int findRepeatNumber(int[] nums) {
        int N = nums.length;
        int[] helper = new int[N + 1];
        List<Integer> list = new ArrayList<Integer>();
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < N; i++) {
            if (helper[nums[i]] == 0) {
                helper[nums[i]]++;
            } else {
                set.add(nums[i]);
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (Integer element : set) {
            stringBuilder.append(element + " 或 ");
        }
        System.out.println(stringBuilder.delete(stringBuilder.length() - 3,stringBuilder.length()).toString());

        return 0;
    }

    public int findRepeatNumber2(int[] nums) {
        int N = nums.length;
        int[] helper = new int[N + 1];
        List<Integer> list = new ArrayList<Integer>();
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < N; i++) {
            if (helper[nums[i]] == 0) {
                helper[nums[i]]++;
            } else {
                set.add(nums[i]);
            }
        }
        System.out.println(set.size());

        return set.size();
    }

    public int findRepeatNumber3(int[] nums) {
        int N = nums.length;
        HashSet<Integer> set = new HashSet<>();
        int ans = -1;
        for (int i = 0; i < N; i++) {
            if (!set.add(nums[i])) {
                ans = nums[i];
            }
        }
        return ans;
    }
}
